var showApplePayBytton = false; var applePayStripeKey = ''; function UpdateApplePayButton(show) { console.log('%%%%%%%%%%%%%%% UpdateApplePayButton', showApplePayBytton, show, !needCardComApplePayButton()); if (showApplePayBytton && show && !needCardComApplePayButton()) { $("#apple_pay").parent().parent().show(); } else { $("#apple_pay").parent().parent().hide(); } } function GetCurrencyShortName(currency) { if (currency && currency > 0) { return currencyShortName[currency].toLowerCase() == 'nis' ? 'ILS' : currencyShortName[currency].toUpperCase(); } } function ApplePayClicked() { if (showApplePayBytton) { usedBit = false; //usedStripeFrame = false; TODO:need to investigate usedApple = true; $('#tbcontacttelephone_star_for_bit').hide(); $('#divForCCDetails').hide(); $('#donate-btn-credit-card').click(); } } function ShowHideApplePayButton() { if (showApplePayBytton && $('#ddlDonationFrequency').val() == 1) { getPublishableKey(); // will show if need } else { UpdateApplePayButton(false) } } function DisableCoverCC() { $("#chbCoverCc").prop("disabled", true); // disable CoverCC checkbox } function EnableCoverCC() { $("#chbCoverCc").prop("disabled", false); // enable CoverCC checkbox } function ProcessApplePay(amount, currency, callback) { try { var currencyName = GetCurrencyShortName(currency); var paymentRequest = { countryCode: 'IL', currencyCode: currencyName, total: { label: 'Donation to ' + _govId, amount: amount } }; var projectId = $("#hdnProjectId").val(); if (projectId === '-1') { if ($("#divProjects").is(":visible") && $("#ddlProjects option").length > 0 && $.trim($("#ddlProjects").val()) !== "") { projectId = $("#ddlProjects").val(); } } Stripe.setPublishableKey(applePayStripeKey); DisableCoverCC(); var session = Stripe.applePay.buildSession(paymentRequest, function (result, completion) { $.post( '/StripeApplePay', { action: '1', govid: _govId, token: result.token.id, amount: amount, currency: currencyName, details: JSON.stringify(result), tipAmount: $('#hdnDonationTipAmount').val(), coverCcAmount: $('#hdnDonationCoverCcAmount').val(), pfpid: $('#hdnPfpId').val(), projectId: projectId } ).done(function (response) { EnableCoverCC(); var parsedResponse = JSON.parse(response); if (parsedResponse.result) { completion(ApplePaySession.STATUS_SUCCESS); $('#hdnApplePayCharge').val(parsedResponse.charge); $('#hdnApplePayDetails').val(JSON.stringify(parsedResponse.details)); $("#hdnPaymentAccountId").val(parsedResponse.accountId); callback(); } else { EnableCoverCC(); completion(ApplePaySession.STATUS_FAILURE); } }).fail(function () { EnableCoverCC(); completion(ApplePaySession.STATUS_FAILURE); }); }, function (error) { alert(error.message); console.log(error.message); }); session.oncancel = function () { EnableCoverCC(); console.log("User hit the cancel button in the payment window"); }; session.begin(); } catch (e) { console.log(e); } } $(document).ready(function () { $('#ddlDonationCurrency').change(function () { UpdateApplePayButton(showApplePayBytton); if (showApplePayBytton) { getPublishableKey(); } }); setAvailabilityCardComApplePay(); // need to be first setAvailability(); function setAvailability() { Stripe.applePay.checkAvailability(function (available) { if (available || qs2("applepay") == 1) { showApplePayBytton = true; getPublishableKey(); // will run UpdateApplePayButton inside } else { UpdateApplePayButton(showApplePayBytton); } console.log('#### UpdateApplePayButton run 1'); UpdateApplePayButton(showApplePayBytton); }); } $("#apple_pay").click(ApplePayClicked); }); function getPublishableKey() { var val = $('#ddlDonationCurrency :selected').val(); if (val != "-1") { var currencyName = GetCurrencyShortName(val); $.post( '/StripeApplePay', { action: '2', govid: _govId, currency: currencyName, pfpid: $('#hdnPfpId').val() } ).done(function (response) { var parsedResponse = JSON.parse(response); if (parsedResponse.result) { applePayStripeKey = parsedResponse.key; Stripe.setPublishableKey(parsedResponse.key); console.log('#### UpdateApplePayButton run 2'); UpdateApplePayButton(true); } else { applePayStripeKey = ''; console.log('#### UpdateApplePayButton run 3'); UpdateApplePayButton(false); } }).fail(function () { Stripe.setPublishableKey(''); applePayStripeKey = ''; console.log('#### UpdateApplePayButton run 4'); UpdateApplePayButton(false); }); } }